﻿/*
 * Created by SharpDevelop.
 * User: Andrew Sadler
 * Date: 03/09/2012
 * Time: 12:09
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using RRRSRoguelike.Helpers.Visualizer;
using RRRSRoguelike.Managers;

namespace RRRSRoguelike.Helpers
{
	/// <summary>
	/// Description of Menu.
	/// </summary>
	public class Menu
	{
		List<MenuItem> MenuItems = new List<MenuItem>();
		int longest;

		public int Selected { get; private set; }

		public void Add(MenuItem menuItem)
		{
			MenuItems.Add(menuItem);

			ArrangeMenuItems();
		}

		public void Clear()
		{
			MenuItems.Clear();
		}

		public void ArrangeMenuItems()
		{
			longest = 0;

			foreach (var mi in MenuItems)
			{
				if (mi.Text.Length > longest)
					longest = mi.Text.Length;
			}

		}

		public void DrawMenu(Panel panel)
		{
			var i = 1;
			var sb = new StringBuilder();
			foreach (var menuItem in MenuItems)
			{
				var consoleColor = i - 1 == Selected ? ConsoleColor.Cyan : ConsoleColor.White;
				sb.Clear();
				sb.Append(i + ". ");
				sb.Append(menuItem.Text);
				panel.Write(sb.ToString(), i - 1, consoleColor);

				i++;
			}
			sb.Append("Current Selection: " + (Selected + 1));
		}

		private long lastCall;
		private long delay = 16;
		public bool HandleInput()
		{
			var change = false;

			if (GameManager.Instance.Time.ElapsedMilliseconds > lastCall + delay)
			{
				lastCall = GameManager.Instance.Time.ElapsedMilliseconds;

				GameManager.Instance.InputManager.ReadKeys();

				if (GameManager.Instance.InputManager.KeyPush(ConsoleKey.UpArrow))
				{
					WrapSelected(-1);
					change = true;
				}
				else if (GameManager.Instance.InputManager.KeyPush(ConsoleKey.DownArrow))
				{
					WrapSelected(1);
					change = true;
				}
				else if (GameManager.Instance.InputManager.KeyPush(ConsoleKey.Enter))
				{
					MenuItems[Selected].Select();
					change = true;
				}
				else if (GameManager.Instance.InputManager.KeyPush(ConsoleKey.Escape))
				{
					change = true;
				}
				switch (GameManager.Instance.InputManager.currentKey.Key)
				{
					case ConsoleKey.D1:
					case ConsoleKey.NumPad1:
						SelectMenuItem(1);
						change = true;
						break;
					case ConsoleKey.D2:
					case ConsoleKey.NumPad2:
						SelectMenuItem(2);
						change = true;
						break;
					case ConsoleKey.D3:
					case ConsoleKey.NumPad3:
						SelectMenuItem(3);
						change = true;
						break;
					case ConsoleKey.D4:
					case ConsoleKey.NumPad4:
						SelectMenuItem(4);
						change = true;
						break;
					case ConsoleKey.D5:
					case ConsoleKey.NumPad5:
						SelectMenuItem(5);
						change = true;
						break;
					case ConsoleKey.D6:
					case ConsoleKey.NumPad6:
						SelectMenuItem(6);
						change = true;
						break;
					case ConsoleKey.D7:
					case ConsoleKey.NumPad7:
						SelectMenuItem(7);
						change = true;
						break;
					case ConsoleKey.D8:
					case ConsoleKey.NumPad8:
						SelectMenuItem(8);
						change = true;
						break;
					case ConsoleKey.D9:
					case ConsoleKey.NumPad9:
						SelectMenuItem(9);
						change = true;
						break;
					case ConsoleKey.D0:
					case ConsoleKey.NumPad0:
						SelectMenuItem(10);
						change = true;
						break;
					case ConsoleKey.Escape:
						SelectMenuItem(MenuItems.Count);
						break;
				}
			}
			return change;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="index">As displayed. 1 is first.</param>
		public void SelectMenuItem(int index)
		{

			if (index > 0 && index <= MenuItems.Count)
			{
				Selected = index - 1;
				MenuItems[index - 1].Select();
			}

		}

		public void WrapSelected(int change)
		{
			Selected += change;
			if (Selected >= MenuItems.Count) Selected = 0;
			else if (Selected < 0) Selected = MenuItems.Count - 1;
		}
	}
}
